//
//  CollectViewController.m
//  Moment
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"

@interface CollectViewController ()

@property(nonatomic,retain)FMDatabase *db;

@end

@implementation CollectViewController

+(instancetype)collectDataBase{
    static CollectViewController *detaBase = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        detaBase = [[CollectViewController alloc]init];
        
    });
    return detaBase;
}


#pragma mark - 打开数据库

-(void)openDB{
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString *fileCollect = [doc stringByAppendingPathComponent:@"collect.sqlite"];
    NSLog(@"base = = = = = %@", fileCollect);
    self.db = [FMDatabase databaseWithPath:fileCollect];
    
    if ([self.db open]) {
        NSLog(@"OK");
    }
    
}


#pragma mark - 创建表
-(void)createCollectListTable{
    
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS myCollect (ID TEXT NOT NULL PRIMARY KEY, imgurl TEXT NOT NULL, title TEXT NOT NULL, type TEXT NOT NULL);"];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"创表成功");
    }else{
        NSLog(@"创表失败");
    }
}



#pragma mark - 插入表
-(void)inserCollectList:(TZYHomeData *)homeModel{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO myCollect (ID, imgurl, title, type) VALUES ('%@', '%@', '%@', '%@')",homeModel._id, homeModel.coverimg, homeModel.title, homeModel.name];
    BOOL result = [self.db executeUpdate:sql];
    
    if (result) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}




#pragma mark - 查看表
-(NSMutableArray *)selectCollectList{
    
    NSMutableArray *array = [NSMutableArray array];
    FMResultSet *resultSet = [self.db executeQuery:@"select * from myCollect"];
    while ([resultSet next]) {
        TZYHomeData *homeModel = [[TZYHomeData alloc]init];
        
        homeModel._id = [resultSet objectForColumnName:@"ID"];
        homeModel.coverimg = [resultSet objectForColumnName:@"imgurl"];
        homeModel.title = [resultSet objectForColumnName:@"title"];
        homeModel.name = [resultSet objectForColumnName:@"type"];
        
        [array addObject:homeModel];
    }
    return array;
}



#pragma mark - 删除数据
-(void)deleteMyCollectData:(NSString *)title{
    
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM myCollect WHERE title = '%@'",title];
    BOOL result = [self.db executeUpdate:deleteSql];
    if (result) {
        
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    
}




#pragma mark - 销毁重建
-(void)dropCollectListTable{
    
    [self.db executeUpdate:@"DROP TABLE IF EXISTS myCollect"];
    
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS myCollect (ID TEXT NOT NULL PRIMARY KEY, imgurl TEXT NOT NULL, title TEXT NOT NULL, type TEXT NOT NULL);"];
    BOOL result = [self.db executeUpdate:sql];
    
    if (result) {
        NSLog(@"再次创表成功");
    }else{
        NSLog(@"再次创表失败");
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
